import { RootThunkAction } from "@/types/store"
import { http } from "@/utils/http"
import {  LoginForm, LoginResponse } from "@/types/data"
import { setToken, clearToken } from "@/utils/token"

export const login = (values: LoginForm): RootThunkAction => {
    return async dispatch => {
        // 这里做请求 然后获取token 然后进行登录
     const res =  await http.post("/authorizations", values) as LoginResponse
     setToken(res.data)
     dispatch({ type: 'login/token', payload: res.data })
    }
}
// 发送验证码
export const sendCode = (mobile: string): RootThunkAction => {
    return  async dispatch => {
       return  http.get(`/sms/codes/${mobile}`)
    }
}
export const logout = (): RootThunkAction => {
    return async (dispatch) => {
      dispatch({ type: 'login/logout' });
      clearToken();
    };
  };